Grokking Python Fundamentals
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Course Overview
Python Basics
Python Overview
Python Application Areas
Your First Python Program
Python - Basic Syntax
Python Comments
Quiz
Python Variables and Operators
Python - Variables
Python - Data types
Python - Numbers
Python - Booleans
Python - Type Conversions
Python - Operators
Quiz
Introduction to Conditional Statements in Python
Python - If...Else
Python - Match Case
Quiz
Introduction to loops in Python
Python - for loop
Python - while loop
Python - Loop Control
Python - for else and while else loop
Quiz
Introduction to Strings in Python
Python - String Operators
Python Slicing Strings
Python - String Methods
Quiz
Introduction to Array in Python
Python - Array Methods
Quiz
Introduction to Lists in Python
Python - Add and Remove List Items
Python - List Comprehension
Python - List Methods
Quiz
Introduction to Tuple in Python
Python - Unpack Tuple Items
Python - Join Tuples
Python - Tuple Methods
Quiz
Introduction to Dictionary in Python
Python - Nested Dictionaries
Python - Dictionary Methods
Quiz
Introduction to set in Python
Python - set Operations
Python - Set Methods
Quiz
Introductions to Function in Python
Python - Function Arguments
Recursive Functions
Python - Variable scope
Importing Modules
Quiz
Python - Variables

Variables are essential in any programming language. They are used to store data values. In Python, variables are created when you assign a value to them, and they don't require explicit declaration to reserve memory space. The variable is created the moment you first assign a value to it.

Creating Python Variables

Creating variables in Python is straightforward; you simply assign a value to a variable name.

Image

Example

Python3
. . . .

Explanation:

  • first_name holds the string "John".
  • age holds the integer 30.
  • height holds the floating point number 5.11.

Printing Python Variables

To output the value of a variable in Python, you can use the print() function. This function sends the data you specify to the standard output, which is typically the console.

Example

Python3

. . . .

Explanation:

  • first_name, age, and height are variables holding the values "John", 30, and 5.11, respectively.
  • The print() function is used to display the values of these variables. Each call to print() outputs the value of the variable specified within the parentheses.

Deleting Python Variables

In Python, you can delete variables from the memory using the del statement. This can be useful when you want to free up memory or ensure that the variable is no longer accessible in later parts of your program.

Example

Python3

. . . .

Explanation:

  • Initially, player_name and player_score are defined and printed.
  • After using the del statement, these variables are removed from memory.
  • Uncommenting the last two print statements would result in an error because the variables no longer exist.

Case-Sensitivity of Python Variables

Python variables are case-sensitive. This means that variables such as Age, age, and AGE are treated as distinct.

Example

Python3

. . . .

Explanation:

  • Each variable (Age, age, AGE) is different due to Python's case sensitivity. They can hold different values without interfering with each other.

Python Variables - Multiple Assignment

Python allows you to assign values to multiple variables in a single line, which can make your code cleaner and faster to write.

Example

Python3

. . . .

Explanation:

  • x, y, and z are simultaneously assigned the values 10, 20, and 30, respectively. This method of multiple assignment is useful for initializing several variables at once.

Python Variables - Naming Convention

When naming variables in Python, it's important to follow certain conventions and rules to ensure that your code is readable and understandable. These conventions also help avoid conflicts with Python's keywords and built-in function names.

Rules for Naming Python Variables:

  1. Variable names should start with a letter or an underscore (_).
  2. Variable names cannot begin with a number.
  3. Variable names can only contain alphanumeric characters and underscores (A-z, 0-9, and _).
  4. Variable names are case-sensitive (age, Age, and AGE are different variables).
  5. Avoid using Python keywords as variable names (e.g., if, else, class, etc.).

Example

Python3

. . . .

Explanation:

  • username, _user_id, and user2name are examples of valid variable names.
  • The commented-out variable names (2user, user-name, and class) illustrate common mistakes that will result in syntax errors.

Python Best Practices for Variable Names:

  • Use Descriptive Names: Variable names should be descriptive to indicate the kind of data they hold. For example, age is better than a, and username is better than usrnm.
  • Use Lowercase for Variables: It is a common practice to use all lowercase letters for variable names, with words separated by underscores if necessary (e.g., user_age).
  • Use CamelCase for Classes: Class names in Python are usually written using CamelCase, where each word starts with a capital letter, without underscores (e.g., UserProfile).

By adhering to these naming conventions and best practices, you can make your Python code more organized and easier for others (and yourself) to read and maintain. This is especially important in collaborative environments or when writing publicly shared code.

Quiz
Python - Data types
Mark as Completed

On this page

Creating Python Variables

Example

Printing Python Variables

Example

Deleting Python Variables

Example

Case-Sensitivity of Python Variables

Example

Python Variables - Multiple Assignment

Example

Python Variables - Naming Convention

Rules for Naming Python Variables:

Example

Python Best Practices for Variable Names: